<?php

namespace app\http\middleware;

use app\facade\Token;
use think\Db;
use app\models\{User as Users, Business};
use app\facade\Redis;

class Sauth
{
    use \traits\controller\Jump;

    public function handle($request, \Closure $next)
    {
        if (!$request->header('token')) {
            $this->result(null, 999, '请登录用户');
        }
        if (!$request->header('shopid')) {
            $this->result(null, 888, '未绑定商户信息');
        }
        $user = $request->user;
        if (empty($user)) {
            $this->result(null, 999, '请登录用户');
        }
        $shopid = $request->header('shopid') ?? 0;
        if (empty($shopid)) {
            $this->result([], 888, '未绑定商户信息');
        }
        $business = (new Business())->where('id', $shopid)->where('status', 1)->find();
        if (empty($business)) {
            $this->result([], 888, '未绑定商户信息');
        }
        if ($business['user_id'] != 0 && $business['user_id'] != $user['id']) {
            $this->result([], 888, '无商户权限');
        }
        $request->shop = $business;
        return $next($request);
    }
}
